package com.portal.cwegportal.controller;

import com.appcenter.jz.Logger.BusinessLogger;
import com.appcenter.jz.Logger.JZLogger;
import com.appcenter.jz.Logger.LoggerGenre;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.config.JsonResult;
import com.portal.cwegportal.input.friendshiplink.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


@RestController
@RequestMapping("api/services/portal/friendshiplink")
@Api(description = "友情链接API")
public class FriendshipLinkController {
    @Autowired
    com.portal.cwegportal.service.FriendshipLinkService friendshipLinkService;

    @ApiOperation(value = "创建友情链接", notes = "创建友情链接", produces = "application/json")
    @PostMapping("/createFriendshipLink")
    public ResponseEntity<JsonResult> createFriendshipLink(@RequestBody CreateFriendshipLinkInput input) throws Exception {
        BusinessLogger.Info(LoggerGenre.Add,"增加了一条友情链接",input);
        JsonResult r = new JsonResult();
        try {

            r.setResult(friendshipLinkService.createFriendshipLink(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "删除友情链接", notes = "删除友情链接",produces = "application/json")
    @PostMapping("/deleteFriendshipLink")
    public ResponseEntity<JsonResult> deleteFriendshipLink(@RequestBody DeleteFriendshipLinkInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"刪除了一条友情链接",input);
        JsonResult r = new JsonResult();
        try {
            r.setResult(friendshipLinkService.deleteFriendshipLink(input));

        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "修改友情链接", notes = "修改友情链接",produces = "application/json")
    @PostMapping("/editFriendshipLink")
    public ResponseEntity<JsonResult> editFriendshipLink(@RequestBody EditFriendshipLinkInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"修改友情链接",input);
        JsonResult r = new JsonResult();
        try {
            r.setResult(friendshipLinkService.editFriendshipLink(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "搜索友情链接", notes = "搜索友情链接",produces = "application/json")
    @PostMapping("/getFriendshipLink")
    public ResponseEntity<JsonResult> getFriendshipLinks(@RequestBody GetFriendshipLinksInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"搜索友情链接",input);
               JsonResult r = new JsonResult();
        try {
            r.setResult(friendshipLinkService.getFriendshipLinks(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "加载友情链接信息", notes = "加载友情链接信息",produces = "application/json")
    @PostMapping("/pageLoad")
    public ResponseEntity<JsonResult> pageLoad(@RequestBody PageLoadInput input) throws Exception {
        BusinessLogger.Info( LoggerGenre.Add,"加载友情链接信息",input);
        JsonResult r = new JsonResult();
        try {
            r.setResult(friendshipLinkService.pageLoad(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }
}
